package com.czy.owner.ui.setting;

import android.content.Intent;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.FrameNumberModel;
import com.czy.owner.entity.UserInfoModel;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.MainActivity;
import com.czy.owner.ui.archive.TakeFrameFrament;
import com.czy.owner.ui.archive.TakeFrameNumberActivity;
import com.czy.owner.ui.archive.TestingDetailsActivity;
import com.czy.owner.ui.archive.TestingListActivity;
import com.czy.owner.utils.ActivityHelper;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.glide.GlideCacheUtil;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.SwitchView;
import com.czy.owner.widget.UniversalDialog;
import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMClient;
import com.igexin.sdk.PushManager;
import com.tencent.bugly.beta.Beta;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2017/5/11 0011.
 */

public class SettingActivity extends BaseActivity {
    @BindView(R.id.swichview_is_notice)
    SwitchView swichviewIsNotice;
    @BindView(R.id.tv_paid_psw_manager)
    TextView tvPaidPswManager;
    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;
    @BindView(R.id.tv_curent_verson)
    TextView tvCurentVerson;
    private UserInfoModel userInfoModel = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.setting);
        tvCacheSize.setText(GlideCacheUtil.getInstance().getCacheSize());
        tvCurentVerson.setText("v" + PhoneUtils.getVersion(SettingActivity.this));
        userInfoModel = UserHelper.getInstance().getUserInfoModel(SettingActivity.this);
        if (TextUtils.isEmpty(userInfoModel.getPayPasswd())) {
            tvPaidPswManager.setText(getString(R.string.set_paid_password));
        } else {
            tvPaidPswManager.setText(getString(R.string.paid_manage));
        }

        if ((int) SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 1) == 1) {
            swichviewIsNotice.setState(true);
        } else {
            swichviewIsNotice.setState(false);
        }
        swichviewIsNotice.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn() {
                swichviewIsNotice.toggleSwitch(true);
                SharedPreferencesUtils.setParam(SettingActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 1);
                PushManager.getInstance().turnOnPush(SettingActivity.this);
                MessageHelper.getInstance().setIsBlockMessage(false);
            }

            @Override
            public void toggleToOff() {
                swichviewIsNotice.toggleSwitch(false);
                SharedPreferencesUtils.setParam(SettingActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 0);
                PushManager.getInstance().turnOffPush(SettingActivity.this);
                MessageHelper.getInstance().setIsBlockMessage(true);
            }
        });
    }

    @Override
    protected void updateViews() {

    }

    @OnClick(R.id.line_update_login_psw)
    public void updateLoginPsw(View view) {
        startActivity(new Intent(SettingActivity.this, UpdateLoginPasswordActivity.class));
    }

    @OnClick(R.id.line_update_paid_psw)
    public void updatePaidPsw(View view) {
        String str = tvPaidPswManager.getText().toString().toString();
        if (str.equals(getString(R.string.setting_paid_psw))) {
            Intent ti = new Intent(SettingActivity.this, SetPaidPasswordActivity.class);
            startActivityForResult(ti, 0x88);
        } else {
            Intent ti = new Intent(SettingActivity.this, PaidManageActivity.class);
            startActivity(ti);
        }
    }

    @OnClick(R.id.line_clean_cache)
    public void cleanCache(View view) {
        cleanCacheDialog("清理缓存", "确定要清理缓存吗?", "确定", "取消");
    }

    private void cleanCacheDialog(String title, String msg, String sure, String cancle) {
        new UniversalDialog(SettingActivity.this).builder().setTitle(title)
                .setMsg(msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        new cleanCacheAsyncTask().execute();
                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }


    // 清除缓存
    class cleanCacheAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mLoadView.ShowLoadView("正在清除缓存");
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                GlideUtils.clearDiskCache(SettingActivity.this);
            } catch (Exception e) {

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tvCacheSize.setText("0.0KB");
            PhoneUtils.ShowToastMessage(SettingActivity.this, "清理成功");
            mLoadView.CancleLoadView();
        }

    }

    @OnClick(R.id.line_check_verson)
    public void checkVersion(View view) {
        Beta.checkUpgrade();
    }

    @OnClick(R.id.btn_exit)
    public void exit(View view) {
//        startActivity();
//        startActivityForResult(new Intent(SettingActivity.this, TakeFrameNumberActivity.class), TakeFrameFrament.SCANNING_FRAME_REQUESTCODE);
        ExitLoginDialog("退出登录", "退出后不会删除任何数据,下次登录依然可以使用本账号", "确定", "取消");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TakeFrameFrament.SCANNING_FRAME_REQUESTCODE && resultCode == TakeFrameFrament.SCANNING_FRAME_RESULTCODE) {
            FrameNumberModel frameNumberModel = (FrameNumberModel) data.getSerializableExtra(TakeFrameFrament.SCANNING_FRAME_INTENT);

        }
    }

    private void ExitLoginDialog(String title, String msg, String sure, String cancle) {
        new UniversalDialog(SettingActivity.this).builder().setTitle(title)
                .setMsg(msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_SESSION_INFO_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_USER_INFO_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_OWNER_INFO_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_CHAIN_STORES_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID);
                        SharedPreferencesUtils.remove(Constants.SHARED_PREFERENCES_KEY_IS_STORE_BIND);
                        ActivityHelper.getInstance().finishAllActivity();
                        UserHelper.getInstance().setUserInfoModel(SettingActivity.this, null);
                        UserHelper.getInstance().setSessionInfoModel(SettingActivity.this, null);
                        UserHelper.getInstance().setOwnerInfoModel(SettingActivity.this, null);
                        UserHelper.getInstance().setmChainStoresList(SettingActivity.this, null);
                        SharedPreferencesUtils.setParam(SettingActivity.this, Constants.SHARED_PREFERENCES_KEY_IS_STORE_BIND_DIALOG, 0);
                        UserHelper.getInstance().setIsLogined(false);
                        EMClient.getInstance().logout(true);
                        Intent ti = new Intent(SettingActivity.this, MainActivity.class);
                        startActivity(ti);

                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }
}
